package cn.schoolwow.workflow.module.task.action.kit;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.workflow.domain.task.CompleteTaskRequest;
import cn.schoolwow.workflow.domain.task.TaskType;
import cn.schoolwow.workflow.domain.task.type.AddSignTaskRequest;
import cn.schoolwow.workflow.domain.task.type.AssignTaskRequest;
import cn.schoolwow.workflow.domain.task.type.BackTaskRequest;
import cn.schoolwow.workflow.module.common.domain.QuickWorkFlowConfig;
import cn.schoolwow.workflow.module.task.action.domain.CompleteTaskOption;
import cn.schoolwow.workflow.module.task.action.service.AddSignTaskServiceFlow;
import cn.schoolwow.workflow.module.task.action.service.AssignTaskServiceFlow;
import cn.schoolwow.workflow.module.task.action.service.BackTaskServiceFlow;
import cn.schoolwow.workflow.module.task.action.service.CompleteTaskServiceFlow;

public class WorkFlowTaskActionServiceImpl implements WorkFlowTaskActionService {
    private QuickWorkFlowConfig quickWorkFlowConfig;

    public WorkFlowTaskActionServiceImpl(QuickWorkFlowConfig quickWorkFlowConfig) {
        this.quickWorkFlowConfig = quickWorkFlowConfig;
    }

    @Override
    public boolean completeTask(long taskId, TaskType taskType) {
        CompleteTaskRequest completeTaskRequest = new CompleteTaskRequest();
        completeTaskRequest.taskId = taskId;
        completeTaskRequest.taskType = taskType;
        return completeTask(completeTaskRequest);
    }

    @Override
    public boolean completeTask(CompleteTaskRequest completeTaskRequest) {
        FlowContext flowContext = quickWorkFlowConfig.workflow.startFlow(new CompleteTaskServiceFlow())
                .putInstanceData(completeTaskRequest)
                .putTemporaryInstanceData(completeTaskRequest.taskType)
                .execute();
        CompleteTaskOption completeTaskOption = flowContext.checkInstanceData(CompleteTaskOption.class);
        return completeTaskOption.instanceFinished;
    }

    @Override
    public void assignWorkFlowTask(AssignTaskRequest assignTaskRequest) {
        quickWorkFlowConfig.workflow.startFlow(new AssignTaskServiceFlow())
                .putInstanceData(assignTaskRequest)
                .execute();
    }

    @Override
    public void backWorkFlowTask(BackTaskRequest backTaskRequest) {
        quickWorkFlowConfig.workflow.startFlow(new BackTaskServiceFlow())
                .putInstanceData(backTaskRequest)
                .execute();
    }

    @Override
    public void addSignWorkFlowTask(AddSignTaskRequest addSignTaskRequest) {
        quickWorkFlowConfig.workflow.startFlow(new AddSignTaskServiceFlow())
                .putInstanceData(addSignTaskRequest)
                .execute();
    }

}
